package com.pachong.frameworkdemo.account;

import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;
import com.pachong.frameworkdemo.app.MyApplication;

/**
 * 账号类
 * <p>
 * 数据直接写到sp里，从内存中读取
 * <p>
 * user_id : test
 * real_name : 真实姓名
 * sign : 签名
 * portrait :
 * phone : 18088888888
 * gender : WOMAN
 * company : 爬虫
 * department : IT事业部
 * position : null
 * ext_info :
 * dateline : 1468217954
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/7/12 16:35
 * @Version: 1.0.0
 */
public class Account {

    private static final String SP_FILENAME_ACCOUNT = Account.class.getSimpleName();
    private static final String KEY_ID = "id";
    private static final String KEY_REAL_NAME = "real_name";
    private static final String KEY_LOGIN_NAME = "login_name";
    private static final String KEY_APP_ID = "app_id";
    private static final String KEY_SIGN = "sign";
    private static final String KEY_PROTRAIT = "portrait";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_GENDER = "gender";
    private static final String KEY_ROLE = "role";
    private static final String KEY_LOCKED = "locked";
    private static final String KEY_CREATE_TIME = "create_time";
    private static final String KEY_UPDATE_TIME = "update_time";
    private static final String KEY_COMPANY = "company";
    private static final String KEY_POSITION = "position";

    @SerializedName("user_id")
    private String mId;
    @SerializedName("login_name")
    private String mLoginName;
    @SerializedName("real_name")
    private String mRealName;
    @SerializedName("app_id")
    private String mAppId;
    @SerializedName("sign")
    private String mSign;
    @SerializedName("portrait")
    private String mPortrait;
    @SerializedName("phone")
    private String mPhone;
    @SerializedName("gender")
    private String mGender;
    @SerializedName("role")
    private String mRole;
    @SerializedName("locked")
    private String mLocked;
    @SerializedName("create_time")
    private String mCreateTime;
    @SerializedName("update_time")
    private String mUpdateTime;
    @SerializedName("company")
    private String mCompany;
    @SerializedName("position")
    private String mPosition;


    public Account() {
    }

    /**
     * 初始化，从SP中读取
     */
    public static Account createFromSp() {
        Account account = new Account();
        account.setRealName(getString(KEY_REAL_NAME));
        account.setId(getString(KEY_ID));
        account.setLoginName(getString(KEY_LOGIN_NAME));
        account.setAppId(getString(KEY_APP_ID));
        account.setSign(getString(KEY_SIGN));
        account.setPortrait(getString(KEY_PROTRAIT));
        account.setPhone(getString(KEY_PHONE));
        account.setGender(getString(KEY_GENDER));
        account.setRole(getString(KEY_ROLE));
        account.setLocked(getString(KEY_LOCKED));
        account.setCreateTime(getString(KEY_CREATE_TIME));
        account.setUpdateTime(getString(KEY_UPDATE_TIME));
        account.setCompany(getString(KEY_COMPANY));
        account.setPosition(getString(KEY_POSITION));
        return account;
    }


    /**
     * 初始化, 克隆一个对象
     */
    public static Account createFrom(Account acct) {
        Account account = new Account();
        account.setRealName(acct.getRealName());
        account.setId(acct.getId());
        account.setLoginName(acct.getLoginName());
        account.setAppId(acct.getAppId());
        account.setSign(acct.getSign());
        account.setPortrait(acct.getPortrait());
        account.setPhone(acct.getPhone());
        account.setGender(acct.getGender());
        account.setRole(acct.getRole());
        account.setLocked(acct.getLocked());
        account.setCreateTime(acct.getCreateTime());
        account.setUpdateTime(acct.getUpdateTime());
        account.setCompany(acct.getCompany());
        account.setPosition(acct.getPosition());
        return account;
    }


    public String getId() {
        return mId;
    }

    public void setId(String id) {
        if (!TextUtils.equals(this.mId, id)) {
            mId = id;
            saveString(KEY_ID, mId);
        }
    }


    public String getRealName() {
        return mRealName;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param name
     */
    public void setRealName(String name) {
        if (!TextUtils.equals(this.mRealName, name)) {
            this.mRealName = name;
            saveString(KEY_REAL_NAME, name);
        }
    }

    public String getLoginName() {
        return mLoginName;
    }

    public void setLoginName(String loginName) {
        if (!TextUtils.equals(this.mLoginName, loginName)) {
            mLoginName = loginName;
            saveString(KEY_LOGIN_NAME, mLoginName);
        }
    }


    public String getAppId() {
        return mAppId;
    }

    public void setAppId(String appId) {
        if (!TextUtils.equals(this.mAppId, appId)) {
            mAppId = appId;
            saveString(KEY_APP_ID, mAppId);
        }
    }

    public String getSign() {
        return mSign;
    }

    public void setSign(String sign) {
        if (!TextUtils.equals(this.mSign, sign)) {
            mSign = sign;
            saveString(KEY_SIGN, mSign);
        }
    }

    public String getPortrait() {
        return mPortrait;
    }

    public void setPortrait(String portrait) {
        if (!TextUtils.equals(this.mPortrait, portrait)) {
            mPortrait = portrait;
            saveString(KEY_PROTRAIT, mPortrait);
        }
    }

    public String getPhone() {
        return mPhone;
    }

    public void setPhone(String phone) {
        if (!TextUtils.equals(this.mPhone, phone)) {
            mPhone = phone;
            saveString(KEY_PHONE, mPhone);
        }
    }

    public String getGender() {
        return mGender;
    }

    public void setGender(String gender) {
        if (!TextUtils.equals(this.mGender, gender)) {
            mGender = gender;
            saveString(KEY_GENDER, mGender);
        }
    }

    public String getRole() {
        return mRole;
    }

    public void setRole(String role) {
        if (!TextUtils.equals(this.mRole, role)) {
            mRole = role;
            saveString(KEY_ROLE, mRole);
        }
    }

    public String getLocked() {
        return mLocked;
    }

    public void setLocked(String locked) {
        if (!TextUtils.equals(this.mLocked, locked)) {
            mLocked = locked;
            saveString(KEY_LOCKED, mLocked);
        }
    }

    public String getCreateTime() {
        return mCreateTime;
    }

    public void setCreateTime(String createTime) {
        if (!TextUtils.equals(this.mCreateTime, createTime)) {
            mCreateTime = createTime;
            saveString(KEY_CREATE_TIME, mCreateTime);
        }
    }

    public String getUpdateTime() {
        return mUpdateTime;
    }

    public void setUpdateTime(String updateTime) {
        if (!TextUtils.equals(this.mUpdateTime, updateTime)) {
            mUpdateTime = updateTime;
            saveString(KEY_UPDATE_TIME, mUpdateTime);
        }
    }

    public String getCompany() {
        return mCompany;
    }

    public void setCompany(String company) {
        if (!TextUtils.equals(this.mCompany, company)) {
            mCompany = company;
            saveString(KEY_COMPANY, mCompany);
        }
    }

    public String getPosition() {
        return mPosition;
    }

    public void setPosition(String position) {
        if (!TextUtils.equals(this.mPosition, position)) {
            mPosition = position;
            saveString(KEY_POSITION, mPosition);
        }
    }


    @Override
    public String toString() {
        return "Account{" +
                "mId='" + mId + '\'' +
                ", mLoginName='" + mLoginName + '\'' +
                ", mRealName='" + mRealName + '\'' +
                ", mAppId='" + mAppId + '\'' +
                ", mSign='" + mSign + '\'' +
                ", mPortrait='" + mPortrait + '\'' +
                ", mPhone='" + mPhone + '\'' +
                ", mGender='" + mGender + '\'' +
                ", mRole='" + mRole + '\'' +
                ", mLocked='" + mLocked + '\'' +
                ", mCreateTime='" + mCreateTime + '\'' +
                ", mUpdateTime='" + mUpdateTime + '\'' +
                ", mCompany='" + mCompany + '\'' +
                ", mPosition='" + mPosition + '\'' +
                '}';
    }

    private static void saveString(String key, String value) {
        SharePreferenceUtil.saveString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static String getString(String key) {
        return SharePreferenceUtil.getString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }

    private static void saveBoolean(String key, Boolean value) {
        SharePreferenceUtil.saveBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static Boolean getBoolean(String key) {
        return SharePreferenceUtil.getBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, false);
    }
}
